﻿using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Shweet.Repository;

namespace Shweet.Engine.Tests
{
    /// <summary>
    ///This is a test class for TeamsTest and is intended
    ///to contain all TeamsTest Unit Tests
    ///</summary>
    [TestClass]
    public class TeamsTest
    {
        [TestMethod]
        public void FollowShweetTeams_NoCurrentTeam_FollowTeam()
        {
            //Arrange
            var teamRepository = new FakeTeamRepository();
            var teamsToAdd = new List<Team>();
            teamsToAdd.Add(new Team {Name = "a", Url = "http://a"});

            //Act
            var teams = new Teams(teamRepository);
            teams.FollowShweetTeams(teamsToAdd);

            //Assert
            Assert.AreEqual(1, teamRepository.GetAll().Count);
            Assert.AreEqual("a", teamRepository.GetAll()[0].Name);
            Assert.AreEqual("http://a", teamRepository.GetAll()[0].Url);
        }

        [TestMethod]
        public void FollowShweetTeams_OneCurrentAddDifferent_FollowNewTeam()
        {
            //Arrange
            var teamRepository = new FakeTeamRepository();
            teamRepository.AddTeam(new Team {Name = "a", Url = "http://a"});

            var teamsToAdd = new List<Team>();
            teamsToAdd.Add(new Team {Name = "b", Url = "http://b"});

            //Act
            var teams = new Teams(teamRepository);
            teams.FollowShweetTeams(teamsToAdd);

            //Assert
            Assert.AreEqual(2, teamRepository.GetAll().Count);
            Assert.AreEqual("b", teamRepository.GetAll()[1].Name);
            Assert.AreEqual("http://b", teamRepository.GetAll()[1].Url);
        }

        [TestMethod]
        public void FollowShweetTeams_OneCurrentAddSame_DontDuplicateFollow()
        {
            //Arrange
            var teamRepository = new FakeTeamRepository();
            teamRepository.AddTeam(new Team {Name = "a", Url = "http://a"});

            var teamsToAdd = new List<Team>();
            teamsToAdd.Add(new Team {Name = "a", Url = "http://a"});

            //Act
            var teams = new Teams(teamRepository);
            teams.FollowShweetTeams(teamsToAdd);

            //Assert
            Assert.AreEqual(1, teamRepository.GetAll().Count);
        }

        [TestMethod]
        public void FollowShweetTeams_OneCurrentAddTwoOneSame_FollowOneNewTeam()
        {
            //Arrange
            var teamRepository = new FakeTeamRepository();
            teamRepository.AddTeam(new Team {Name = "a", Url = "http://a"});

            var teamsToAdd = new List<Team>();
            teamsToAdd.Add(new Team {Name = "a", Url = "http://a"});
            teamsToAdd.Add(new Team {Name = "b", Url = "http://b"});

            //Act
            var teams = new Teams(teamRepository);
            teams.FollowShweetTeams(teamsToAdd);

            //Assert
            Assert.AreEqual(2, teamRepository.GetAll().Count);
            Assert.AreEqual("a", teamRepository.GetAll()[0].Name);
            Assert.AreEqual("http://a", teamRepository.GetAll()[0].Url);
            Assert.AreEqual("b", teamRepository.GetAll()[1].Name);
            Assert.AreEqual("http://b", teamRepository.GetAll()[1].Url);
        }

        [TestMethod]
        public void FollowShweetTeams_NoTeamsToAddCurrentlyNotFollowing_ZeroFollowing()
        {
            // Arrange
            var teamRepository = new FakeTeamRepository();

            // Act
            var teams = new Teams(teamRepository);
            teams.FollowShweetTeams(new List<Team>());

            Assert.AreEqual(0, teamRepository.GetAll().Count);
        }

        [TestMethod]
        public void Discover_OneTeamFoundNotFollowing_ReturnOneTeamNotFollowed()
        {
            //Arrange
            var teamRepository = new FakeTeamRepository();
            teamRepository.AddDiscoveredTeam(new Team {Name = "a", Url = "http://a"});

            //Act
            var teams = new Teams(teamRepository);
            List<Team> shweetTeams = teams.DiscoverShweetTeams();

            //Assert
            Assert.AreEqual(1, shweetTeams.Count);
            Assert.IsFalse(shweetTeams[0].Following);
        }

        [TestMethod]
        public void Discover_OneTeamFoundAlreadyFollowing_ReturnOneTeamFollowed()
        {
            //Arrange
            var teamRepository = new FakeTeamRepository();
            teamRepository.AddDiscoveredTeam(new Team {Name = "a", Url = "http://a"});
            teamRepository.AddTeam(new Team {Name = "a", Url = "http://a"});

            //Act
            var teams = new Teams(teamRepository);
            List<Team> shweetTeams = teams.DiscoverShweetTeams();

            //Assert
            Assert.AreEqual(1, shweetTeams.Count);
            Assert.IsTrue(shweetTeams[0].Following);
        }

        [TestMethod]
        public void UnFollowShweetTeams_OneTeamToRemove_CallRepositoryRemoveWithTeam()
        {
            //Arrange
            var teamRepository = new FakeTeamRepository();
            List<Team> teamToUnFollow = new List<Team> {new Team {Name = "a", Url = "http://a"}};

            //Act
            var teams = new Teams(teamRepository);
            teams.UnFollowShweetTeams(teamToUnFollow);

            //Assert
            Assert.IsTrue(teamRepository.RemoveWasCalled);
            Assert.AreEqual(teamToUnFollow,teamRepository.UnfollowedTeams);
        }
    }

    internal class FakeTeamRepository : ITeams
    {
        private readonly List<Team> _discoveredteams;
        private readonly List<Team> _teams;


        public FakeTeamRepository()
        {
            _teams = new List<Team>();
            _discoveredteams = new List<Team>();
        }

        #region ITeams Members

        public void Add(List<Team> teams)
        {
            _teams.AddRange(teams);
        }

        public void Remove(List<Team> teams)
        {
            RemoveWasCalled = true;
            UnfollowedTeams = teams;
        }

        public List<Team> GetAll()
        {
            return _teams;
        }

        public List<Team> Discover()
        {
            return _discoveredteams;
        }

        #endregion

        public void AddTeam(Team team)
        {
            _teams.Add(team);
        }

        public void AddDiscoveredTeam(Team team)
        {
            _discoveredteams.Add(team);
        }

        public bool RemoveWasCalled { get; set;}

        public List<Team> UnfollowedTeams { get; set; }
    }
}